﻿using Pdelvo.Minecraft.Network;

namespace Pdelvo.Minecraft.Protocol.Packets
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    public class AddObject : Packet
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="AddObject"/> class.
        /// </summary>
        /// <remarks></remarks>
        public AddObject()
        {
            Code = 0x17;
        }

        /// <summary>
        /// Gets or sets the entity ID.
        /// </summary>
        /// <value>The entity ID.</value>
        /// <remarks></remarks>
        public int EntityID { get; set; }
        /// <summary>
        /// Gets or sets the type.
        /// </summary>
        /// <value>The type.</value>
        /// <remarks></remarks>
        public byte Type { get; set; }
        /// <summary>
        /// Gets or sets the X.
        /// </summary>
        /// <value>The X.</value>
        /// <remarks></remarks>
        public int X { get; set; }
        /// <summary>
        /// Gets or sets the Y.
        /// </summary>
        /// <value>The Y.</value>
        /// <remarks></remarks>
        public int Y { get; set; }
        /// <summary>
        /// Gets or sets the Z.
        /// </summary>
        /// <value>The Z.</value>
        /// <remarks></remarks>
        public int Z { get; set; }
        /// <summary>
        /// Gets or sets the fireball thrower.
        /// </summary>
        /// <value>The fireball thrower.</value>
        /// <remarks></remarks>
        public int FireballThrower { get; set; }
        /// <summary>
        /// Gets or sets the unknown X.
        /// </summary>
        /// <value>The unknown X.</value>
        /// <remarks></remarks>
        public short UnknownX { get; set; }
        /// <summary>
        /// Gets or sets the unknown Y.
        /// </summary>
        /// <value>The unknown Y.</value>
        /// <remarks></remarks>
        public short UnknownY { get; set; }
        /// <summary>
        /// Gets or sets the unknown Z.
        /// </summary>
        /// <value>The unknown Z.</value>
        /// <remarks></remarks>
        public short UnknownZ { get; set; }

        /// <summary>
        /// Receives the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        public override void Receive(BigEndianStream reader, int version)
        {
            EntityID = reader.ReadInt();
            Type = reader.ReadByte();
            X = reader.ReadInt();
            Y = reader.ReadInt();
            Z = reader.ReadInt();
            if ((FireballThrower = reader.ReadInt()) > 0)
            {
                UnknownX = reader.ReadShort();
                UnknownY = reader.ReadShort();
                UnknownZ = reader.ReadShort();
            }
        }

        /// <summary>
        /// Sends the specified writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="version">The version.</param>
        /// <remarks></remarks>
        protected override void Send(BigEndianStream writer, int version)
        {
            writer.Write(Code);
            writer.Write(EntityID);
            writer.Write(Type);
            writer.Write(X);
            writer.Write(Y);
            writer.Write(Z);
            writer.Write(FireballThrower);
            if (FireballThrower > 0)
            {
                writer.Write(UnknownX);
                writer.Write(UnknownY);
                writer.Write(UnknownZ);
            }
        }
    }
}